<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manager extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->library('Huashang/staff');
		$this->staff->checkLogin();
	}
	public function index(){
		$this->load->view('manager/list',$data);
	}
	public function staff(){
		$data['auth']=explode(',', $this->input->get('auth'));
		$this->load->view('manager/staff',$data);
	}

	public function stafflist(){
		$rs=$this->staff->getuser();
	    echo json_encode($rs['msg']);
	}

	private function getHierarchy($rows, $id='id',$pid='pid',$child='child',$root=0){
		$tree = array();
		if(is_array($rows)){
            $array = array();
            foreach ($rows as $key=>$item){
				$array[$item[$id]] =& $rows[$key];
			}
			foreach($rows as $key=>$item){
				$parentId = $item[$pid];
				if($root == $parentId){
					$tree[] =&$rows[$key];
				}else{
					if(isset($array[$parentId])){
						$parent =&$array[$parentId];
						$parent[$child][]=&$rows[$key];
					}
				}
			}
		}
		return $tree;
	}
	public function ajaxrole(){
		$rs=array_pop($this->staff->roletree());
		foreach($rs as $k=>$v){
			$rs[$k]['open']=false;
		}
		echo json_encode($rs);
	}

	public function add(){
		$this->edit();
	}
	//根据部门获取员工
	public function getRoleStaff(){
		$id = $this->input->get('rid');
		$rs = array_pop($this->staff->getRoleStaff($id));
		echo json_encode($rs);
	}
	public function edit(){
		$this->load->library('Huashang/wechat');
		$data['rs']               = array_pop($this->staff->info($this->input->get('id')));
		$data['parent']           = array_pop($this->staff->info($this->session->userdata('admin_id')));
		$data['role']             = array_pop($this->staff->roleinfo($this->input->get('rid')));
		$data['rs']['privileges'] = $data['rs']['privileges']?$data['rs']['privileges']:$data['role']['privileges'];
		$data['wechat']           = array_pop($this->wechat->alllist());
		$data['rs']['pid']        = $this->input->get('pid')?$this->input->get('pid'):$data['rs']['pid'];
		$data['scope_site']       = explode(',', $data['rs']['scope_site']); 
		$this->load->view('manager/edit',$data);
	}
	public function roleauth(){
		$role_id = $this->input->get('id');
		$data['role'] = array_pop($this->staff->roleinfo($role_id));
		if(!$data['role']['privileges']){
			$arr = array_pop($this->staff->roleinfo($data['parent']['role_id']));
			$data['role']['privileges'] = $arr['privileges'];
		}
		$data['public_auth']      = preg_replace("/\\s/is",'',file_get_contents('static/data/public_menu.json'));
		$data['pc_auth']          = preg_replace("/\\s/is",'',file_get_contents('static/data/pc_menu.json'));
		$data['wechat_auth']      = preg_replace("/\\s/is",'',file_get_contents('static/data/wechat_menu.json'));
		$data['app_auth']         = preg_replace("/\\s/is",'',file_get_contents('static/data/app_menu.json'));
		$data['market_auth']      = preg_replace("/\\s/is",'',file_get_contents('static/data/market_menu.json'));
		
		$contain = json_decode($data['role']['privileges'], true); 
		$arr = array('public','pc','app','wechat','market');
		foreach($arr as $s){
			$rows = json_decode($data[$s.'_auth'],true); 
			foreach($rows as $k=>$v){
				if(array_key_exists($v['en_name'],$contain[$s])){
					foreach($v['submenu'] as $a=>$b){
						if(!array_key_exists($b['en_name'], $contain[$s][$v['en_name']])){
							unset($rows[$k]['submenu'][$a]);
						}
					}
				}else{
					unset($rows[$k]);
				}
			}
			$rs[$s]=$rows;
		}
		echo json_encode($rs);
	}
	
	public function save(){
		$this->staff->save();
	}
	public function delete(){
		//$this->crud->db->where_in('id', explode(',',$this->input->post('id')));
		$this->staff->remove();
		//$this->crud->db->delete('admin');
	}
	/***************角色*************************/
	public function roles(){
		$this->load->view('admin/manager/role');
	}
	//获取角色列表
	public function getlist(){
		$arr = array_pop($this->staff->getroles($this->session->userdata('role_id')));
		$rs = array_pop($this->staff->roleinfo($this->session->userdata('role_id')));
		echo json_encode($this->getHierarchy($arr,'id','pid','child',$rs['pid']));
	}
	//修改员工及权限
	public function edit_role(){
		$data['auth']=explode(',', $this->input->get('auth'));
		$this->load->library('Huashang/wechat');
		$data['rs']          = array_pop($this->staff->roleinfo($this->input->get('id')));
		$data['wechat']      = array_pop($this->wechat->getlist());
		$data['rs']['pid']   = $this->input->get('pid')?$this->input->get('pid'):$data['rs']['pid'];
		$data['scope_site']  = explode(',', $data['rs']['scope_site']); 
		$id                  = $this->input->get('id');
		$this->staff->getPrivileges(is_numeric($this->input->get('id'))?'EDIT':'ADD',is_numeric($this->input->get('id'))?$this->input->get('id'):$this->input->get('pid'),$data);
		$arr                 = array_pop($this->staff->roleinfo($this->session->userdata('role_id')));
		$data['role_pid']    = $arr['pid'];
		$this->load->view('manager/edit_role',$data);
	}
	//保存员工及权限
	public function save_role(){
		$this->staff->saverole();
	}
	public function delete_role(){
		$this->staff->delRole($this->input->post('id'));
	}
	public function getusers(){
		$rs=$this->staff->getuser(array('role_id'=>'value:'.$this->input->get('rid')));
		echo json_encode($rs['msg']);
	}
	/****************微信列表管理*******************/
	public function sites(){$this->load->view('manager/wechat');}
	
	public function getsites(){
		$this->load->library('Huashang/wechat');
		$wechat=$this->wechat->getlist();
		echo json_encode($wechat['msg']);
	}
	
	public function edit_sites(){
		$this->load->library('Huashang/wechat');
		$rs=$this->wechat->update();
		$data['rs']=$rs['msg'];
		$this->load->view('manager/edit_sites',$data);
	}
	public function save_sites(){
		$this->load->library('Huashang/wechat');
		$this->wechat->save();
	}
	public function delete_sites(){
		$this->load->library('Huashang/wechat');
		$this->wechat->remove();
	}
	
	public function notes(){
		$this->load->view('manager/notes');
	}
	public function password(){
		$this->load->view('manager/password');
	}
	public function save_pwd(){
		$rs=$this->staff->changepwd();
		exit($rs['status']);
	}
	public function games(){
		$this->load->view('admin/manager/games');
	}
	public function qr_reader(){
		$text = $this->input->get('text');
		echo file_get_contents('http://api.qrserver.com/v1/read-qr-code/?fileurl='.urlencode(str_replace('/admin/../','/',$text)));
	}
}